package afcp.alumni.email;

import java.io.IOException;
import java.io.StringWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import afcp.alumni.util.Conf;

public class MailTemplate {

	private static final Log logger = LogFactory.getLog(MailTemplate.class);

	private static String resourcePath(String resource) {
		return MailTemplate.class.getClassLoader().getResource(resource)
				.getPath();
	}

	static {
		Velocity.init(resourcePath("velocity.properties"));
	}

	private static Template passwordTemplateHtml = null;
	private static Template passwordTemplateTxt = null;
	private static final String PASSWORD_TEMPLATE_HTML_PATH = "mail/logins.html";
	private static final String PASSWORD_TEMPLATE_TXT_PATH = "mail/logins.txt";
	private static final String FIRSTNAME_KEY = "firstName";
	private static final String LOGIN_KEY = "login";
	private static final String GENERATED_PASSWORD_KEY = "generatedPassword";

	private MailTemplate() {
	}
	
	private static Template getPasswordTemplateHtml() {
		if (passwordTemplateHtml == null) {
			passwordTemplateHtml = Velocity.getTemplate(PASSWORD_TEMPLATE_HTML_PATH,
					Conf.DEFAULT_CHARSET_NAME);
		}
		return passwordTemplateHtml;
	}
	private static Template getPasswordTemplateTxt() {
		if (passwordTemplateTxt == null) {
			passwordTemplateTxt = Velocity.getTemplate(PASSWORD_TEMPLATE_TXT_PATH,
					Conf.DEFAULT_CHARSET_NAME);
		}
		return passwordTemplateTxt;
	}

	public static String getPasswordTemplateHtml(String firstName,
			String login, String generatedPassword) {
		return getPasswordTemplate(firstName, login, generatedPassword,
				getPasswordTemplateHtml());
	}

	public static String getPasswordTemplateTxt(String firstName, String login,
			String generatedPassword) {
		return getPasswordTemplate(firstName, login, generatedPassword,
				getPasswordTemplateTxt());
	}

	public static String getPasswordTemplate(String firstName, String login,
			String generatedPassword, Template template) {
		VelocityContext context = new VelocityContext();
		context.put(FIRSTNAME_KEY, firstName);
		context.put(LOGIN_KEY, login);
		context.put(GENERATED_PASSWORD_KEY, generatedPassword);
		StringWriter writer = new StringWriter();
		template.merge(context, writer);
		writer.flush();
		try {
			writer.close();
		} catch (IOException e) {
			logger.error("Could not close password template String writer.", e);
		}
		return writer.toString();
	}

	public static String[] getPasswordTemplateHtmlTxt(String firstName,
			String login, String generatedPassword) {
		VelocityContext context = new VelocityContext();
		context.put(FIRSTNAME_KEY, firstName);
		context.put(LOGIN_KEY, login);
		context.put(GENERATED_PASSWORD_KEY, generatedPassword);
		
		StringWriter writer = new StringWriter();
		getPasswordTemplateHtml().merge(context, writer);
		writer.flush();
		try {
			writer.close();
		} catch (IOException e) {
			logger.error("Could not close password template String writer.", e);
		}
		String html = writer.toString();
		
		writer.getBuffer().setLength(0);
		getPasswordTemplateTxt().merge(context, writer);
		writer.flush();
		try {
			writer.close();
		} catch (IOException e) {
			logger.error("Could not close password template String writer.", e);
		}
		String txt = writer.toString();
		
		return new String[] {html, txt};
	}
}
